/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.ComponentModel;
using System.Windows.Forms;
using CrSyncFilesForms.Properties;
using CrSyncFilesForms.Utils;
using CrSyncFilesUtils;


namespace CrSyncFilesForms {
    public partial class CrSyncUiChangeOption : Form {
        public CrSyncUiChangeOption() {
            InitializeComponent();
        }

        public CrSyncUiChangeOption(FileSyncPairInfo folderPair) {
            InitializeComponent();
            FolderPair = folderPair;
            PairManager = ApplicationGlobals.PairManager;
            Text = Resources.TEXT_CHANGE_OPTIONS_FOR + FolderPair.FolderPairName;
            Init();
            FormClosing += CallBackClosing;
        }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        public FileSyncPairInfo FolderPair { get; set; }
        public FileSyncPairManager PairManager { get; set; }

        public bool _optionsChanged;


        private void Init() {
            TextBoxInclusion.Text = FolderPair.GetSearchPattern();
            TextBoxExclusion.Text = FolderPair.GetExcludePattern();

            CheckExcludeReadOnlyFiles.Checked = FolderPair.FileExcludeAttrReadOnly;
            CheckExcludeHiddenFiles.Checked = FolderPair.FileExcludeAttrHidden;
            CheckExcludeSystemFiles.Checked = FolderPair.FileExcludeAttrSystem;
            CheckActiveForRunAll.Checked = FolderPair.ActiveForRunAll;
            CheckSaveOverWrittenInRecyclebin.Checked = FolderPair.OptionsBackupOlderFiles;
            CheckFileContents.Checked = FolderPair.OptionsCompareFileStreams;
        }

        private bool ShouldCancel()
        {
            if (_optionsChanged)
            {
                return MessageBox.Show(Resources.MESSAGE_OPTIONS_CHANGED,
                                       Resources.TEXT_SHORT_NAME,
                                       MessageBoxButtons.YesNo,
                                       MessageBoxIcon.Question) == DialogResult.Yes;
            }
            return true;
        }

        private void CallBackCrSyncUiChangeOptionLoad(object sender, EventArgs e)
        {
            // Color scheme
            BackColor = CRSyncColors.DialogBackGround;
            LabelTitle.ForeColor = CRSyncColors.Title;
            LabelInclud.ForeColor = CRSyncColors.ForeColor;
            LabelExclude.ForeColor = CRSyncColors.ForeColor;
            CheckExcludeHiddenFiles.ForeColor = CRSyncColors.ForeColor;
            CheckExcludeReadOnlyFiles.ForeColor = CRSyncColors.ForeColor;
            CheckExcludeSystemFiles.ForeColor = CRSyncColors.ForeColor;
            CheckFileContents.ForeColor = CRSyncColors.ForeColor;
            CheckSaveOverWrittenInRecyclebin.ForeColor = CRSyncColors.ForeColor;
            CheckActiveForRunAll.ForeColor = CRSyncColors.ForeColor;
            LinkSelectSubFolder.LinkColor = CRSyncColors.LinkForeColor;
            groupBox1.ForeColor = CRSyncColors.ForeColor;

            _optionsChanged = false;

            AcceptButton = ButtonOK;
            //CancelButton = ButtonCancel;
        }

        private void CallBackOk(object sender, EventArgs e)
        {

            FolderPair.SetSearchPattern(TextBoxInclusion.Text);
            FolderPair.SetExcludePattern(TextBoxExclusion.Text);
            FolderPair.FileExcludeAttrReadOnly = CheckExcludeReadOnlyFiles.Checked;
            FolderPair.FileExcludeAttrHidden = CheckExcludeHiddenFiles.Checked;
            FolderPair.FileExcludeAttrSystem = CheckExcludeSystemFiles.Checked ;
            FolderPair.ActiveForRunAll = CheckActiveForRunAll.Checked;
            FolderPair.OptionsBackupOlderFiles = CheckSaveOverWrittenInRecyclebin.Checked;
            FolderPair.OptionsCompareFileStreams = CheckFileContents.Checked;
            DialogResult = DialogResult.OK;
        }

        private void CallBackCancel(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }

        private void CallBackClosing(object sender, FormClosingEventArgs args)
        {
            if (DialogResult == DialogResult.Cancel)
            {
                if (!ShouldCancel())
                {
                    args.Cancel = true;
                }
                
            }
        }

        private void CallBackSelectSubFolder(object sender, LinkLabelLinkClickedEventArgs e) {
            var subFolderTreeViewer = new CrSyncUiSubFolders(FolderPair);
            if(subFolderTreeViewer.ShowDialog(this) == DialogResult.OK )
            {
                //FolderPair = subFolderTreeViewer.FolderPair;
                FolderPair.SubDirectoryExclusionList = subFolderTreeViewer.SubFoldersExclusionList;
                ApplicationGlobals.PairManager.UpdateFolderPair(FolderPair);
                ApplicationGlobals.SyncToyManager.UpdateFolderPair(FolderPair);
            }
        }

        private void CallBackFilesToIncludeChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void FilesToExcludeChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void CallBackExcludeReadOnlyChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void CallBackExcludeHiddenChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void CallBackExcludeSystemChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void CallBackRunAllActionChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void CallBackSaveOverwrittenChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void CallBackCheckContentsChanged(object sender, EventArgs e)
        {
            _optionsChanged = true;
        }

        private void CallBackKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                CallBackCancel(this, new EventArgs());
            }
        }

        
    }
}
